﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;

namespace View.Setting
{
    public partial class AnalogSetForm : DevExpress.XtraEditors.XtraForm
    {
        private BindingList<AnalogCS> analog = new BindingList<AnalogCS>();

        public AnalogSetForm()
        {
            InitializeComponent();
            this.gridControl1.DataSource = analog;
            InitTypeList();
        }

        private void InitTypeList()
        {
            treeView_type.Nodes.Clear();

            foreach (string strKey in c_Device.DeviceMsgGather.Keys)
            {
                TreeNode subNode = treeView_type.Nodes.Add(strKey);
                foreach (objMsgAttriStruct attri in c_Device.DeviceMsgGather[strKey].ArrtiList)
                {
                    subNode.Nodes.Add(attri.AttriName);
                }
            }

            if (treeView_type.Nodes.Count > 0 && treeView_type.Nodes[0].Nodes.Count > 0)
                treeView_type.SelectedNode = treeView_type.Nodes[0].Nodes[0];

        }

        private void ComboBox_pro_SelectedIndexChanged(object sender, EventArgs e)
        {
            analog.Clear();
            //(this.comboBox_pro.SelectedItem as objMsgDevStruct).AnalogSet
            objSingleAnalogStruct obj =c_Device.GetSingleAnalogConfig(treeView_type.SelectedNode.Parent.Text, this.comboBox_pro.SelectedItem.ToString(), treeView_type.SelectedNode.Text);
            if (obj == null)
                return;

            AnalogCS cs = new AnalogCS();
            cs.strName = "单位";
            cs.strContext = obj.UnitName;
            analog.Add(cs);

            cs = new AnalogCS();
            cs.strName = "报警上限";
            cs.strContext = obj.UpperThreshold.ToString();
            analog.Add(cs);

            cs = new AnalogCS();
            cs.strName = "报警下限";
            cs.strContext = obj.LowerThreshold.ToString();
            analog.Add(cs);

            this.gridView1.RefreshData();

            txtTest.Text = c_RealTimeValue.GetRealTimeAnalogValue(treeView_type.SelectedNode.Parent.Text, this.comboBox_pro.SelectedItem.ToString(), treeView_type.SelectedNode.Text).Value.ToString();
        }

        private void InitDevList(string strType)
        {
            this.comboBox_pro.Items.Clear();
            foreach (objMsgDevStruct dev in c_Device.DeviceMsgGather[strType].DevList)
            {
                this.comboBox_pro.Items.Add(dev);
            }

            this.comboBox_pro.SelectedIndex = 0;
        }

        private void treeView_type_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                InitDevList(e.Node.Parent.Text);
            }
        }

        private void groupBox2_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox2.BackColor);
            e.Graphics.DrawString(groupBox2.Text, groupBox2.Font, Brushes.DarkBlue, 10, 1);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.DarkBlue, e.Graphics.MeasureString(groupBox2.Text, groupBox2.Font).Width + 8, 7, groupBox2.Width - 2, 7);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, 7, 1, groupBox2.Height - 2);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, groupBox2.Height - 2, groupBox2.Width - 2, groupBox2.Height - 2);
            e.Graphics.DrawLine(Pens.DarkBlue, groupBox2.Width - 2, 7, groupBox2.Width - 2, groupBox2.Height - 2); 
        }

        /// <summary>
        /// 重绘GroupBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox1.BackColor);
            e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, Brushes.DarkBlue, 10, 1);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.DarkBlue, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 8, 7, groupBox1.Width - 2, 7);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, 7, 1, groupBox1.Height - 2);
            e.Graphics.DrawLine(Pens.DarkBlue, 1, groupBox1.Height - 2, groupBox1.Width - 2, groupBox1.Height - 2);
            e.Graphics.DrawLine(Pens.DarkBlue, groupBox1.Width - 2, 7, groupBox1.Width - 2, groupBox1.Height - 2); 
        }

        /// <summary> 
        /// 修改参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnModifyCS_Click(object sender, EventArgs e)
        {
            float outValue =0;
            if(!float.TryParse(txtCurent.Text,out outValue))
            {
                MessageBox.Show("请输入正确的实际值");
                return;
            }

            List<objCmdModifyCfgType> obj = new List<objCmdModifyCfgType>();
            objCmdModifyCfgType cfg = new objCmdModifyCfgType();
            AnalogObj analogObj = c_Device.GetAnalogDevNo(this.treeView_type.SelectedNode.Parent.Text,
                (this.comboBox_pro.SelectedItem as objMsgDevStruct).DevName,
                this.treeView_type.SelectedNode.Text);
            cfg.AnalogNo = (short)analogObj.devNo;
            cfg.AnalogType = (byte)analogObj.type;

            cfg.Modified_Up = GetModifyValue("报警上限");
            cfg.Modified_Low = GetModifyValue("报警下限");
            cfg.CurrentValue = float.Parse(txtTest.Text);
            cfg.TargetValue = outValue;
            obj.Add(cfg);
            bool flag = c_SendData.SendCmdModifyCFG(obj);
            if (!flag)
                MessageBox.Show("修改参数未成功");
        }

        /// <summary>
        /// 获取编辑后数据
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        private float GetModifyValue(string strName)
        {
            BindingList<AnalogCS> analogTemp = this.gridControl1.DataSource as BindingList<AnalogCS>;

            foreach (AnalogCS cd in analogTemp)
            {
                if (cd.strName == strName)
                    return float.Parse(cd.strContext);
            }

            return -1;
        }


        [Serializable]
        public class AnalogCS
        {
            public AnalogCS()
            {
                strName = string.Empty;
                strContext = string.Empty;
            }
            public string strName { get; set; }
            public string strContext { get; set; }

        }


    }
}